package codeRandomThoughts.Test198打家劫舍;

public class Solution2 {
    public int rob(int[] nums) {
        //dp[i]:从0-i号房屋中偷东西,偷到的最高金额为dp[i]
        int[] dp = new int[nums.length];

        dp[0] = nums[0];
        if (nums.length < 2) {
            return dp[nums.length - 1];
        }
        dp[1] = Math.max(nums[0], nums[1]);

        for (int i = 2; i < nums.length; i++) {
            //两种情况
            //1.偷第i号房屋
            //2.不偷第i号房屋
            dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
        }

        return dp[nums.length - 1];
    }
}
